package com.hqd.jjvm.classformat.attributeinfo;

import com.hqd.jjvm.classformat.ClassFile;
import com.hqd.jjvm.util.HexStrTransformUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * 常量表达式
 *
 * @author hqd
 */
@Getter
@NoArgsConstructor
public class ConstantValue extends AttributeInfo {
    /**
     * 常量所在常量池的下标
     */
    private Integer constantValueIndex;

    public ConstantValue(Integer attributeNameIndex, Integer attributeLength, Integer constantValueIndex) {
        super(attributeNameIndex, attributeLength);
        this.constantValueIndex = constantValueIndex;
    }

    @Override
    protected ConstantValue newAttributeInfo(Integer attributeNameIndex, Integer attributeLength, ClassFile classFile) {
        this.constantValueIndex = HexStrTransformUtil.parseHexToInt(classFile.readU2());
        return new ConstantValue(attributeNameIndex, attributeLength, constantValueIndex);
    }
}
